package pl.register.android.activities.program.logic;

import android.content.Context;
import pl.register.android.activities.program.ui.ProgramView;
import pl.register.android.chapter.ChapterTitle;
import pl.register.android.classes.ClassesTopic;
import pl.register.android.course.Course;
import pl.register.android.database.DatabaseAdapter;
import pl.register.android.database.dao.CourseDao;
import pl.register.android.database.dao.ProgramDao;
import pl.register.android.database.exception.NotFoundCourseException;
import pl.register.android.homework.HomeworkTopic;
import pl.register.android.lesson.LessonProgram;
import pl.register.android.preferences.ResotorePreferences;
import pl.register.android.program.Program;
import pl.register.android.year.Year;
import pl.register.android.year.YearType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-09
 */

public class ProgramPresenter {

    private DatabaseAdapter databaseAdapter;

    private ProgramView programView;

    private ProgramDao programDao;

    private CourseDao courseDao;


    private Course displayCourse;

    private Year displayYear;

    private Map<YearType, Year> yearMap;

    public ProgramPresenter(DatabaseAdapter databaseAdapter, ProgramView programView) {
        this.databaseAdapter = databaseAdapter;
        this.programDao = databaseAdapter.getProgramDao();
        this.courseDao = databaseAdapter.getCourseDao();
        this.programView = programView;
        this.yearMap = new HashMap<YearType, Year>();
    }

    public void initWith(Course course, Map<YearType, Year> yearMap) {
        this.displayCourse = course;
        this.yearMap = yearMap;
    }

    public void populateViews(YearType selectedYearType) {
        displayYear = yearMap.get(selectedYearType);
        if (displayCourse != null) {
            programView.populateCourse("" + displayCourse.getStartYear(), "" + displayCourse.getEndYear());
        }
        if (displayYear != null) {
            programView.refreshTitle(displayYear);
            programView.populateProgram(displayYear, displayYear.getProgram());
        }
    }

    public void rebuildForNextCourse(Context context) throws NotFoundCourseException {
        Course nextCourse = new Course(displayCourse.getStartYear() + 1, displayCourse.getEndYear() + 1);
        courseDao.findCourseFor(nextCourse.getStartYear(), nextCourse.getEndYear());
        new AsyncProgramsLoader(context, programView, this, nextCourse).execute();
    }

    public void rebuildForPreviousCourse(Context context) throws NotFoundCourseException {
        Course previousCourse = new Course(displayCourse.getStartYear() - 1, displayCourse.getEndYear() - 1);
        courseDao.findCourseFor(previousCourse.getStartYear(), previousCourse.getEndYear());
        new AsyncProgramsLoader(context, programView, this, previousCourse).execute();
    }


    public void changeYearType(Context context, YearType yearType) {
        displayYear = yearMap.get(yearType);
        if (displayYear != null) {
            programView.refreshTitle(displayYear);
            programView.populateProgram(displayYear, displayYear.getProgram());
        }
        ResotorePreferences.saveYearType(context, yearType);
    }

    public void saveLessonProgram(LessonProgram lessonProgram) {
        programDao.updateLessonProgram(lessonProgram);
        programView.refresh(displayYear);
    }

    public void saveClassesTopic(ClassesTopic classesTopic) {
        programDao.updateClassesTopic(classesTopic);
        programView.refresh(displayYear);
    }

    public void saveHomeworkTopic(HomeworkTopic homeworkTopic) {
        programDao.updateHomeworkTopic(homeworkTopic);
        programView.refresh(displayYear);
    }

    public void saveHolidayLessonProgram(LessonProgram lessonProgramHoliday1) {
        Program holiday1Program = yearMap.get(YearType.HOLIDAY_I_YEAR).getProgram();
        Program holiday2Program = yearMap.get(YearType.HOLIDAY_II_YEAR).getProgram();
        List<LessonProgram> holiday1LessonProgram = holiday1Program.getLessons();
        List<LessonProgram> holiday2LessonProgram = holiday2Program.getLessons();
        for (int i = 0; i < holiday1LessonProgram.size(); i++) {
            LessonProgram lessonProgram = holiday1LessonProgram.get(i);
            if(lessonProgramHoliday1.equals(lessonProgram)){
                LessonProgram lessonProgramHoliday2 = holiday2LessonProgram.get(i);
                ClassesTopic classesHoliday1 = lessonProgramHoliday1.getClassesTopics().get(0);
                ClassesTopic classesHoliday2 = lessonProgramHoliday2.getClassesTopics().get(0);
                ChapterTitle chapterHoliday1 = lessonProgramHoliday1.getChapterTitle();
                ChapterTitle chapterHoliday2 = lessonProgramHoliday2.getChapterTitle();

                classesHoliday2.setTopic(classesHoliday1.getTopic());
                chapterHoliday2.setTitle(chapterHoliday1.getTitle());
                programDao.updateLessonProgram(lessonProgramHoliday1);
                programDao.updateLessonProgram(lessonProgramHoliday2);
                programView.refresh(displayYear);
                return;
            }
        }
    }

    public DatabaseAdapter getDatabaseAdapter() {
        return databaseAdapter;
    }
}